package com.android.personal.finance;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.android.personal.data.FinanceDataAdaptater;
import com.android.personal.data.FinanceDataTags;
import com.android.personal.edit.AccountsEditActivity;
import com.android.personal.finance.R;

public class AccountsActivity extends ListActivity {

	private Cursor accountsCursor;

	private static final int EDIT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;
	
    private static final int ACCOUNT_CREATE = 0;
    private static final int ACCOUNT_EDIT = 1;

	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accounts_layout);

		fillData();

		registerForContextMenu(getListView());
	}

	/**
	 * Options Menu Overrides.
	 * 
	 */

	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.accounts_options_menu, menu);
		return true;

	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.new_account:
			createAccount();
			return true;
		}
		return false;
	}

	/*************************************************/

	/**
	 * Context Options Menu Overrides.
	 * 
	 */

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, EDIT_ID, 0, R.string.mn_edit);
		menu.add(0, DELETE_ID, 0, R.string.mn_delete);
	}

	public boolean onContextItemSelected(MenuItem item) {

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();

		switch (item.getItemId()) {
		case EDIT_ID:
			editAccount(info.position);
			return true;
		case DELETE_ID:
			deleteAccount(info.position);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	/*************************************************/
	
	/**
	 * Intent Results Overrides.
	 * 
	 */
	
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		
        super.onActivityResult(requestCode, resultCode, intent);
        
        if (resultCode == RESULT_CANCELED)
        	return ;
        
        Bundle extras = intent.getExtras();
        
        String name = extras.getString(FinanceDataTags.ACCOUNT_NAME);
        String balance = extras.getString(FinanceDataTags.ACCOUNT_BALANCE);
        
        switch(requestCode) {
        case ACCOUNT_CREATE:
        	FinanceDataAdaptater.createAccount(name, balance);
            break;
        case ACCOUNT_EDIT:
            String rowId = extras.getString(FinanceDataTags.ACCOUNT_KEY);
            if (rowId != null)
            	FinanceDataAdaptater.updateAccount(rowId, name, balance);
            break;
        }
        
        fillData();
    }
	
	/*************************************************/

	/**
	 * Local functions.
	 * 
	 */

	private void createAccount() {

		Intent i = new Intent(this, AccountsEditActivity.class);
		startActivityForResult(i, ACCOUNT_CREATE);
	}

	private void editAccount(int position) {

		Intent i = new Intent(this, AccountsEditActivity.class);

		Cursor c = accountsCursor;
		c.moveToPosition(position);

		i.putExtra(FinanceDataTags.ACCOUNT_KEY, c.getString(c
				.getColumnIndexOrThrow(FinanceDataTags.ACCOUNT_KEY)));
		i.putExtra(FinanceDataTags.ACCOUNT_NAME, c.getString(c
				.getColumnIndexOrThrow(FinanceDataTags.ACCOUNT_NAME)));
		i.putExtra(FinanceDataTags.ACCOUNT_BALANCE, c.getString(c
				.getColumnIndexOrThrow(FinanceDataTags.ACCOUNT_BALANCE)));

		startActivityForResult(i, ACCOUNT_EDIT);
	}

	private void deleteAccount(int position) {

		Cursor c = accountsCursor;
		c.moveToPosition(position);

		FinanceDataAdaptater.deleteAccount(c.getLong(c
				.getColumnIndexOrThrow(FinanceDataTags.ACCOUNT_KEY)));
		
		fillData();
	}

	private void fillData() {
		
		accountsCursor = FinanceDataAdaptater.fetchAllAccounts();
		startManagingCursor(accountsCursor);

		String[] from = new String[] { FinanceDataTags.ACCOUNT_NAME,
				FinanceDataTags.ACCOUNT_BALANCE };
		int[] to = new int[] { R.id.account_row, R.id.account_balance };

		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.account_row, accountsCursor, from, to);
		setListAdapter(notes);
	}

}
